{
 "metadata": {
  "name": "",
  "signature": "sha256:0f49f37a40a28a0d3ba337bfdb46edce4cb26bb9feb7d031feadad777242e320"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "# Liang Zhenfeng"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "# MTH9879 Homework 3\n",
      "\n",
      "Assigned: February 17, 2015.\n",
      "Due: March 3, 2015 by 6pm. \n",
      "\n",
      "Late homework **will not be accepted**.\n",
      "\n",
      "$$\n",
      "\\newcommand{\\supp}{\\mathrm{supp}}\n",
      "\\newcommand{\\E}{\\mathbb{E}}\n",
      "\\newcommand{\\Eof}[1]{\\mathbb{E}\\left[ #1 \\right]}\n",
      "\\def\\Cov{{ \\mbox{Cov} }}\n",
      "\\def\\Var{{ \\mbox{Var} }}\n",
      "\\newcommand{\\1}{\\mathbf{1} }\n",
      "\\newcommand{\\PP}{\\mathbb{P} }\n",
      "%\\newcommand{\\Pr}{\\mathrm{Pr} }\n",
      "\\newcommand{\\QQ}{\\mathbb{Q} }\n",
      "\\newcommand{\\RR}{\\mathbb{R} }\n",
      "\\newcommand{\\DD}{\\mathbb{D} }\n",
      "\\newcommand{\\HH}{\\mathbb{H} }\n",
      "\\newcommand{\\spn}{\\mathrm{span} }\n",
      "\\newcommand{\\cov}{\\mathrm{cov} }\n",
      "\\newcommand{\\sgn}{\\mathrm{sgn} }\n",
      "\\newcommand{\\HS}{\\mathcal{L}_{\\mathrm{HS}} }\n",
      "%\\newcommand{\\HS}{\\mathrm{HS} }\n",
      "\\newcommand{\\trace}{\\mathrm{trace} }\n",
      "\\newcommand{\\LL}{\\mathcal{L} }\n",
      "%\\newcommand{\\LL}{\\mathrm{L} }\n",
      "\\newcommand{\\s}{\\mathcal{S} }\n",
      "\\newcommand{\\ee}{\\mathcal{E} }\n",
      "\\newcommand{\\ff}{\\mathcal{F} }\n",
      "\\newcommand{\\hh}{\\mathcal{H} }\n",
      "\\newcommand{\\bb}{\\mathcal{B} }\n",
      "\\newcommand{\\dd}{\\mathcal{D} }\n",
      "\\newcommand{\\g}{\\mathcal{G} }\n",
      "\\newcommand{\\p}{\\partial}\n",
      "\\newcommand{\\half}{\\frac{1}{2} }\n",
      "\\newcommand{\\T}{\\mathcal{T} }\n",
      "\\newcommand{\\bi}{\\begin{itemize}}\n",
      "\\newcommand{\\ei}{\\end{itemize}}\n",
      "\\newcommand{\\beq}{\\begin{equation}}\n",
      "\\newcommand{\\eeq}{\\end{equation}}\n",
      "\\newcommand{\\beas}{\\begin{eqnarray*}}\n",
      "\\newcommand{\\eeas}{\\end{eqnarray*}}\n",
      "\\newcommand{\\cO}{\\mathcal{O}}\n",
      "\\newcommand{\\cF}{\\mathcal{F}}\n",
      "\\newcommand{\\cL}{\\mathcal{L}}\n",
      "\\newcommand{\\BS}{\\text{BS}}\n",
      "$$"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<font color = \"red\">Homework is to be done by each student individually.  To receive full credit, you must email a completed copy of this iPython notebook to Yu Gan (yugan323@gmail.com), Fubo Shi (fubo.shi.baruch@gmail.com), and Tai-Ho Wang (tai-ho.wang@baruch.cuny.edu) by the due date and time.  All R-code must run correctly and solutions must be written up neatly in Markdown/LaTeX format.\n",
      "\n",
      "<font color=\"blue\">If you encounter problems with Markdown/LaTeX or iPython notebook, please contact your TAs Yu Gan and/or Fubo Shi.\n"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "### 0. (0 points) \n",
      "Read Chapter 11 of Hasbrouck and Section 3.5 of Foucault, Pagano and R&ouml;ell. <br>\n",
      "Also, read the article on high-frequency trading by Menkveld."
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "## The Garman (1976) model"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "### 1. (6 points) \n",
      "Suppose supply and demand functions satisfy\n",
      "\n",
      "$$\n",
      "P_A(\\lambda) = 101 \u2212 18\\lambda + \\lambda^2; \\; P_B(\\lambda) = 61 + 2\\lambda + \\lambda^2,\n",
      "$$\n",
      "\n",
      "where $\\lambda$ is the market order arrival rate per minute and $P_B(\\cdot)$ and $P_A(\\cdot)$ denote supply and demand curves respectively. <br>\n",
      "Determine the dealer\u2019s optimal bid $B$ and ask $A$ and the average profit per minute."
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "###### Solution\n",
      "\n",
      "Given a $\\lambda$, the profit of the dealer is \n",
      "$$\n",
      "\\Pi = \\lambda * (P_A - P_B) = \\lambda * (101 - 18 \\lambda + \\lambda^2 - 61 - 2 \\lambda - \\lambda^2) = \\lambda * (40 - 20 \\lambda) = -20 \\lambda^2 + 40\\lambda\n",
      "$$\n",
      "This is a parabolic function, when \n",
      "$$\n",
      "\\lambda = - 40 / (2 * -20) = 1\n",
      "$$\n",
      "dealer can maximize its profit.\n",
      "\n",
      "So,\n",
      "$$\n",
      "P_B = P_B(1) = 61 + 2 + 1 = 64\n",
      "$$\n",
      "\n",
      "$$\n",
      "P_A = P_A(1) = 101 - 18 + 1 = 84\n",
      "$$\n",
      "\n",
      "$$\n",
      "\\Pi = \\Pi(1) = -20 + 40 = 20\n",
      "$$"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "## The Stoll (1978) model"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "### 2. (4 points) \n",
      "In the Stoll (1978) model, assuming exponential utility with $S \\sim N(\\mu, \\sigma^2)$, and with current inventory $q$ shares, what are the dealer\u2019s indifference bid and ask prices for $n$ shares?"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Suppose the bid price is B. \n",
      "\n",
      "If the bid price is not hit, the terminal wealth is $ W = qS.$ \n",
      "\n",
      "If the bid price is hit, the terminal wealth is $W = (q + n)S - nB$\n",
      "$$\n",
      "\\E[U((q+n)S-nB)]=\\E[U(qS)]\n",
      "$$\n",
      "\n",
      "This is a moment generating function of normal variable,we take the exponential term out,\n",
      "\n",
      "$$ \n",
      "(q+n)\\E[S]-nB-\\frac{\\alpha}{2}\\Var[(q+n)S]=q\\E[S]-\\frac{\\alpha}{2}\\Var[qS]\n",
      "$$\n",
      "\n",
      "So,\n",
      "\n",
      "$$ \n",
      "B = \\mu-\\frac{\\alpha}{2n}\\,\\left[(q+n)^2-q^2\\right]\\,\\sigma^2 = \\mu-\\frac{\\alpha}{2}\\,(2\\,q+n)\\,\\sigma^2.\n",
      "$$\n",
      "\n",
      "Similarly, for the ask price\n",
      "$$\n",
      "\\E[U((q-n)S + nA)]=\\E[U(qS)]\n",
      "$$\n",
      "\n",
      "We take the exponential term out,\n",
      "$$ \n",
      "(q-n)\\E[S] + nA -\\frac{\\alpha}{2}\\Var[(q-n)S]=q\\E[S]-\\frac{\\alpha}{2}\\Var[qS]\n",
      "$$\n",
      "\n",
      "$$ \n",
      "A = \\mu + \\frac{\\alpha}{2n}\\,\\left[(q-n)^2-q^2\\right]\\,\\sigma^2 = \\mu-\\frac{\\alpha}{2}\\,(2\\,q-n)\\,\\sigma^2.\n",
      "$$"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "### 3. (10 points)\n",
      "As mentioned in Lecture 3, in real markets, bid and ask prices $B$ and $A$ are typically given and the dealer must decide how much size to quote at these fixed prices.  Suppose there are currently $Q_A$ offered and $Q_B$ shares bid.  According to Lecture 1,\n",
      "\n",
      "$$\n",
      "\\Eof{S} \\approx M + \\frac s 2 \\,\\frac 4\\pi \\,\\arctan I.\n",
      "$$\n",
      "\n",
      "where $M$ is the mid-price, $s=A-B$, and the book imbalance is given by\n",
      "\n",
      "$$\n",
      "I = \\frac{Q_B-Q_A}{Q_B + Q_A}.\n",
      "$$\n",
      "\n",
      "Assume that $S$ is normally distributed with variance $\\sigma^2$, that the dealer has exponential utility, and that his current inventory is $q$.\n",
      "\n",
      "(a) Use your answer to Problem 2 to show that the indifference size $n_B$ the dealer should quote at the bid $B$ is given by\n",
      "\n",
      "$$\n",
      "n_B = \\frac{s}{\\alpha\\,\\sigma^2}\\,\\left(  1+\\frac 4 \\pi \\,\\arctan I \\right) - 2\\,q.\n",
      "$$\n",
      "\n",
      "(c) Derive a similar formula for the indifference offered quantity $n_A$.\n",
      "\n",
      "(b) Give your intuition for the sensitivities of $n_B$ and $n_A$ to each of the various inputs.\n"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "###### (a)\n",
      "From problem 2, we know,\n",
      "\n",
      "$$ \n",
      "B = \\mu-\\frac{\\alpha}{2}\\,(2\\,q+n_B)\\,\\sigma^2.\n",
      "$$\n",
      "\n",
      "Rearrange the equation,\n",
      "\n",
      "$$\n",
      "n_B = \\frac{2(\\mu - B)}{\\alpha \\sigma^2} - 2q \\\\\n",
      "    = \\frac{2(M + \\frac{s}{2} \\frac 4\\pi \\arctan I - B)}{\\alpha \\sigma^2} - 2q \\\\ \n",
      "    = \\frac{s}{\\alpha\\,\\sigma^2}\\,\\left(  1+\\frac 4 \\pi \\,\\arctan I \\right) - 2\\,q\n",
      "$$"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "##### (b)\n",
      "We know,\n",
      "\n",
      "$$ \n",
      "A = \\mu-\\frac{\\alpha}{2}\\,(2\\,q-n_A)\\,\\sigma^2 \\\\\n",
      "$$\n",
      "\n",
      "Rearrange it,\n",
      "$$\n",
      "n_A = 2q - \\frac{2(\\mu - A)}{\\alpha \\sigma^2} \\\\\n",
      "    = 2q - \\frac{2(M + \\frac{s}{2} \\frac 4\\pi \\arctan I - A)}{\\alpha \\sigma^2} \\\\ \n",
      "    = 2q - \\frac{2}{\\alpha\\,\\sigma^2}\\,\\left( \\frac{s}{2} \\frac 4 \\pi \\,\\arctan I - M \\right)\n",
      "$$"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "##### (c)\n",
      "Notice the indifference is to keep the marginal utility between inventory and the cash equal.\n",
      "\n",
      "For $n_B$\n",
      "\n",
      "(1) $\\,s$ increases, $\\, n_B$ increases, because bid price is relative low, so the decrement of utility because the B we spend is less than the increament of utility from the inventory until the the their marginal utilities hold equal.\n",
      "\n",
      "(2) $q$ decreases, $n_B$ increases. When the current inventory is low, the marginal utility of inventory is high, we need to increase $n_B$ so as the marginal utility decrease to be equal.\n",
      "\n",
      "(3) The book imbalance increases, $n_B$ increases. The increament of the I means current market size of the bid trade size is relative high, so that the bid price is relatie low, which becomes the B decreases, same as (1).\n",
      "\n",
      "(4) $\\sigma$ decreases, $n_B$ increases. When variance decreases, the asset becomes safer, dealers prefer to have more inventory.\n",
      "\n",
      "(5) $\\alpha$ decreses, $n_B$ increases. When the $\\alpha$ decreases, the utility is less sensitive with respect to the wealth, so we need more inventory to keep the indifference.\n",
      "\n",
      "For $n_A$\n",
      "\n",
      "(1) q goes up, $n_A$ goes up. Marginal utility of A is low, we need to sell more to get it down.\n",
      "\n",
      "(2) s goes up, $n_A$ goes down. A relatively high, marginal utility of gettng one unit of A is high, we need less.\n",
      "\n",
      "(3) I goes up, $n_A$ goes down. $Q_A$ relatively low, $A$ relatively high, same as (2).\n",
      "\n",
      "(4) $\\sigma$ goes up, $n_A$ goes up. Inventory become riskier, we holds less.\n",
      "\n",
      "(5) $\\alpha$ goes up, $n_A$ goes up. More sensitive, we need to sell more."
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "## Avellaneda and Stoikov (2008)"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "### 4. (6 points) \n",
      "According to the lecture slides, in the Avellaneda and Stoikov model, the optimal bid and ask prices are given in terms of market order arrival rates by\n",
      "\n",
      "$$\n",
      "B=r_B-\\frac{1}{\\alpha}\\,\\log\\left(1 - \\alpha\\,\\frac{{\\lambda_B}(S-B)}{\\lambda_B'(S-B)}\\right).\n",
      "$$\n",
      "\n",
      "Similarly, the optimal ask price is given by\n",
      "\n",
      "$$\n",
      "A = r_A + \\frac{1}{\\alpha}\\,\\log\\left(1 - \\alpha\\,\\frac{{\\lambda_A}(A-S)}{\\lambda_A'(A-S)}\\right).\n",
      "$$\n",
      "\n",
      "where $A= S+\\delta_A$ and $B= S-\\delta_B$.\n",
      "\n",
      "Suppose that arrival rates of orders (symmetric between market buys and sells) are given by\n",
      "\n",
      "$$\n",
      "\\lambda(\\delta) = e^{-k \\delta \\pm \\beta\\,q}\n",
      "$$\n",
      "\n",
      "for some constant $\\beta>0$ that reflects adverse selection (i.e., the longer you are, the more market sell orders arrive).\n",
      "\n",
      "(a) Write down expressions for $A$ and $B$ in terms of $r_A$, $r_B$, $k$, $\\beta$ and $q$.\n",
      "\n",
      "(b) How does the optimal spread depend on $q$?\n",
      "\n",
      "(c) Conjecture how the reservation prices $r_A$ and $r_B$ should depend on $q$ if there is adverse selection.\n"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "##### (a)\n",
      "\n",
      "$$\n",
      "B = r_B-\\frac{1}{\\alpha}\\,\\log\\left(1 - \\alpha\\,\\frac{{\\lambda_B}(S-B)}{\\lambda_B'(S-B)}\\right) \\\\\n",
      "  = r_B - \\frac{1}{\\alpha} \\log(1 - \\alpha \\frac{e^{-k(S - B) \\pm \\beta q}}{k e^{-k(S - B) \\pm \\beta q}}) \\\\\n",
      "  = r_B - \\frac{1}{\\alpha} \\log(1 - \\alpha \\frac{1}{k})\n",
      "$$\n",
      "\n",
      "$$\n",
      "A = r_A + \\frac{1}{\\alpha}\\,\\log\\left(1 - \\alpha\\,\\frac{{\\lambda_A}(A-S)}{\\lambda_A'(A-S)}\\right) \\\\\n",
      "  = r_A + \\frac{1}{\\alpha} \\log(1 + \\alpha \\frac{1}{k})\n",
      "$$"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "##### (b)\n",
      "$$\n",
      "s = A - B \\\\\n",
      "  = r_A + \\frac{1}{\\alpha} \\log(1 + \\alpha \\frac{1}{k}) - r_B + \\frac{1}{\\alpha} \\log(1 - \\alpha \\frac{1}{k})\n",
      "$$\n",
      "\n",
      "which shows the spread is independent with q"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "##### (c)\n",
      "\n",
      "If there is adverse selection, when the q decrease, $r_B$ decreases. Because we have a small amount of q, we have little information about this asset, we are not willing to buy it at a high price.\n",
      "\n",
      "When q increases, $r_A$ decreases. The dealers are the sellers. They have a lot of q while the market buyer would be willing to pay less for these because of the adverse selection. So, the expectation of the sellers is going down."
     ]
    }
   ],
   "metadata": {}
  }
 ]
}